﻿#include "choosecadre.h"
#include "ui_choosecadre.h"
#include "cadresearch.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlQuery>

ChooseCadre::ChooseCadre(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChooseCadre)
{
    ui->setupUi(this);
    this->setWindowTitle("参数查找");
    this->setGeometry(600,300,700,500);
    this->setWindowFlags(Qt::WindowStaysOnTopHint);
    initTableWidget();
}

ChooseCadre::~ChooseCadre()
{
    delete ui;
}

//初始化tableWidget
void ChooseCadre::initTableWidget()
{
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);  //设置为可以选中单个
}

//处理接收到的数据
void ChooseCadre::dealData(QString idNum)
{
    //allStatus = 0;//程序异常结束
    QString sql = QString("select CadreName,Birth,IDNum,Sex,UnitID from CadreInfo where IDNum = '%1'").arg(idNum);
    QSqlQuery query;
    query.exec(sql);
    while(query.next()) //一行一行遍历
    {
        int rowc = ui->tableWidget->rowCount();//获取行数
        ui->tableWidget->insertRow(rowc);
        //选择框
        QTableWidgetItem *check = new QTableWidgetItem();
        check->setCheckState(Qt::Unchecked); //设置首列为 CheckBox 控件
        ui->tableWidget->setItem(rowc,0,check);
        //干部名称
        ui->tableWidget->setItem(rowc,1,new QTableWidgetItem(query.value(0).toString()));
        //出生时间
        ui->tableWidget->setItem(rowc,2,new QTableWidgetItem(query.value(1).toString()));
        //证件号码
        ui->tableWidget->setItem(rowc,3,new QTableWidgetItem(query.value(2).toString()));
        //性别
        if(query.value(3).toInt()== 1)
            ui->tableWidget->setItem(rowc,4,new QTableWidgetItem("男"));
        if(query.value(3).toInt()== 0)
            ui->tableWidget->setItem(rowc,4,new QTableWidgetItem("女"));
        //单位
        QString sqlUnit = QString("select unitname from unit where unitid = '%1'").arg(query.value(4).toString());
        QSqlQuery queryUnit;
        queryUnit.exec(sqlUnit);
        while(queryUnit.next()) //一行一行遍历
        {
            ui->tableWidget->setItem(rowc,5,new QTableWidgetItem(queryUnit.value(0).toString()));
        }
    }
    int newrows = ui->tableWidget->rowCount();//获取行数
    QString newtext = QString(" 共: %1 条").arg(newrows);
    ui->label_2->setText(newtext);

}

//查询
void ChooseCadre::clearTableWidget()
{
    ui->tableWidget->setRowCount(0);
}

//确定添加
void ChooseCadre::on_buttonSure_clicked()
{
    int rows = ui->tableWidget->rowCount();
    for(int i=0;i<rows;i++)
    {
        if(ui->tableWidget->item (i,0)->checkState()==Qt::Checked)
        {
            QString idNum = ui->tableWidget->item (i,3)->text();
            //qDebug()<<idNum;
            emit sendNum(idNum);
        }
    }
    ui->tableWidget->setRowCount(0);
    this->close();
}

//删除
void ChooseCadre::on_buttonDel_clicked()
{
    QTableWidgetItem * item = ui->tableWidget->currentItem();

    if(item == 0) return;

    ui->tableWidget->removeRow(item->row());

    int newrows = ui->tableWidget->rowCount();//获取行数
    QString newtext = QString(" 共: %1 条").arg(newrows);
    ui->label_2->setText(newtext);

}

//全选
void ChooseCadre::on_buttonAll_clicked()
{
    if(allStatus == 0)
    {
        int rows = ui->tableWidget->rowCount();
        for(int i=0;i<rows;i++)
        {
            ui->tableWidget->item(i,0)->setCheckState(Qt::Checked);
        }

        allStatus = 1;
    }
    else
    {
        int rows = ui->tableWidget->rowCount();
        for(int i=0;i<rows;i++)
        {
            ui->tableWidget->item(i,0)->setCheckState(Qt::Unchecked);
        }

        allStatus = 0;
    }
}

//查找
void ChooseCadre::on_buttonFind_clicked()
{
    int newrows = 0;
    int rows = ui->tableWidget->rowCount();
    QString gbName = ui->lineEdit->text();
    if ("" == gbName)
    {   //判断输入是否为空
        for (int i = 0; i < rows; i++)
        {
            newrows = rows;
            ui->tableWidget->setRowHidden(i, false);//显示所有行          
        }
    }
    else
    {
        QList <QTableWidgetItem *> item = ui->tableWidget->findItems(gbName, Qt::MatchContains);

        for (int i = 0; i < rows; i++)
        {
            ui->tableWidget->setRowHidden(i, true);//隐藏所有行
        }

        if (!item.isEmpty())
        {
            newrows = item.count();
            for (int i = 0; i < item.count(); i++)
            {
                ui->tableWidget->setRowHidden(item.at(i)->row(),false);//item.at(i).row()输出行号
            }
        }
    }
    QString newtext = QString(" 共: %1 条").arg(newrows);
    ui->label_2->setText(newtext);
}
